package me.xiaoyan.ehr.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import me.xiaoyan.ehr.dao.LoginUserRepository;
import me.xiaoyan.ehr.po.Account;
import me.xiaoyan.ehr.po.Employee;
import me.xiaoyan.ehr.service.AccountService;
import me.xiaoyan.ehr.service.VerifyTokenService;
import me.xiaoyan.ehr.vo.ApiResponseData;
import me.xiaoyan.ehr.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class LoginController {

    private final int codeExpireTime = 15;
    private AccountService accountService;
    private VerifyTokenService verifyTokenService;
    @Resource
    private LoginUserRepository loginUserRepository;

    @Autowired
    public void setVerifyTokenService(VerifyTokenService verifyTokenService) {
        this.verifyTokenService = verifyTokenService;
    }

    @Autowired
    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }

    @PostMapping("/login")
    @ApiOperation(
            value = "登录", httpMethod = "POST", notes = "账号登录",
            response = String.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(required = true, name = "username", value = "账号", dataType = "String"),
            @ApiImplicitParam(required = true, name = "password", value = "密码", dataType = "String")
    })
    public ApiResponseData<LoginUser> login(String username, String password) {
        ApiResponseData<LoginUser> data = new ApiResponseData<>();
        if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return data.createError(1, "用户名和密码不能为空");
        }
        try {
            Thread.sleep(500);
            Account acc = accountService.login(username, password);
            LoginUser emp = new LoginUser();
            emp.setId(acc.getId());
            emp.setEmployee(acc.getEmployee());

            loginUserRepository.save(emp); // 将对象直接存储到redis
            data.setData(emp);
            return data;
        } catch (Exception e) {
            return data.createError(2, e.getMessage());
        }
    }

    @GetMapping("/send-code")
    @ApiOperation(
            value = "发送验证码", httpMethod = "GET", response = String.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(required = true, name = "email", value = "邮箱", dataType = "String")
    })
    public ApiResponseData<String> sendResetCode(@RequestParam String email) {
        ApiResponseData data = new ApiResponseData();
        verifyTokenService.sendCode(email, codeExpireTime);
        return data;
    }

    @GetMapping("/update-pwd")
    public ApiResponseData updateLoginPwd(@RequestParam String email,@RequestParam String token,@RequestParam String newPassword){
        ApiResponseData data = new ApiResponseData();
        accountService.updatePwd(email, token,newPassword);
        return data;
    }
}
